package com.example.repository;

import com.example.entity.Permission;
import com.example.entity.User;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface UserRepository {

    @Select("select * from user")
    @Results(value = {
            @Result(property = "id",column = "id",jdbcType = JdbcType.INTEGER,id = true),
            @Result(property = "name",column = "name",jdbcType = JdbcType.VARCHAR),
            @Result(property = "permissionList",column = "id",many = @Many(select = "com.example.repository.UserRepository.findAllPermissionByUId"))
    })
    public List<User> findAllPermission();

    @Select("SELECT p.* FROM USER u, user_role ur , role r,role_permission rp ,permission p \n" +
            "WHERE u.`id` = ur.`user_id` \n" +
            "AND ur.`role_id` = r.`id` \n" +
            "AND r.`id` = rp.`role_id` \n" +
            "AND rp.`permission_id` = p.`id` \n" +
            "AND u.`id` = #{id} \n" +
            "GROUP BY rp.`permission_id`")
    public List<Permission> findAllPermissionByUId(int id);
}
